package cn.gson.crm.controller.counselor;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.gson.crm.controller.BaseServlet;
import cn.gson.crm.model.dao.StudentPoDao;
import cn.gson.crm.model.entity.Employee;
import cn.gson.crm.model.po.FollowStudentPo;

@WebServlet("/counselor/myfavor")
public class MyFavorServlet extends BaseServlet{
	/**
	 * 我的协助列表
	 */
	private static final long serialVersionUID = 1L;
	private StudentPoDao stuPoDao = new StudentPoDao();

	@Override
	protected void get(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Employee emp = (Employee) request.getSession().getAttribute("emplo");
		String p = request.getParameter("page");
		if(p==null||p.isEmpty()){
			p="1";
		}
		Integer page = Integer.parseInt(p);
		String stuName = request.getParameter("stuName");
		List<FollowStudentPo> followStuList1 = stuPoDao.getMyAssitStudentList(emp.getId(), 1, "已分配", stuName);
		List<FollowStudentPo> followStuList2 = null;
		if(followStuList1!=null){
			followStuList2 = new ArrayList<>();
			for (FollowStudentPo po : followStuList1) {
				List<FollowStudentPo> recordList = stuPoDao.getFollowRecordById(po.getId(), emp.getId());
				if(recordList!=null&&recordList.size()>0){
					po.setNextTime(recordList.get(0).getNextTime());
				}
				followStuList2.add(po);
			}
		}
		Integer totalPage = stuPoDao.getAssitStuTotalPage(emp.getId(), 1,"已分配", stuName);
		List<FollowStudentPo> followStuList = stuPoDao.getListThenDividPage(request, followStuList2, page);
		request.setAttribute("page", page);
		request.setAttribute("totalPage", totalPage);
		request.setAttribute("queryString", noPageQueryString(request) );
		request.setAttribute("totalPage", totalPage);
		request.setAttribute("followStuList",followStuList);
		getServletPathAndForward(request,"counselor/cou_manage.jsp");
	}
}
